import { getToken } from '@/utils/cookie'
import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/c-oj/login',
      name: 'login',
      component: () => import('@/views/Login.vue'),
    },
    {
      // 添加一个重定向
      path: '/',
      redirect: '/c-oj/home/question',
    },
    {
      path: '/c-oj/home',
      name: 'home',
      component: () => import('@/views/Home.vue'),
      children: [
        {
          path: 'question',
          name: 'question',
          component: () => import('@/views/Question.vue'),
          meta: {showBanner: true}
        },
        {
          path: 'exam',
          name: 'exam',
          component: () => import('@/views/Exam.vue'),
          meta: {showBanner: true}
        },
        {
          path: 'user/exam',
          name: 'userExam',
          component: () => import('@/views/UserExam.vue'),
          meta: {showBanner: false}
        },
        {
          path: 'user/detail',
          name: 'userDetail',
          component: () => import('@/views/UserDetail.vue'),
          meta: {showBanner: false}
        }
      ]
    },
    {
      path: '/c-oj/anwser',
      name: 'answer',
      component: () => import('@/views/Answer.vue'),
    },
  ],
})

// router.beforeEach((to, from, next) =>{
//   //在每个请求跳转是进行一下token的判断
//   //用于解决 - 1、未登录时，能够直接访问后台菜单页面
//   //2、登录后，在token未过期时，用户又想进入登录页面登录
//   if(getToken()){
//     // has token 
//     if(to.path === "/c-oj/login"){
//       next({path: "/c-oj/home"})
//     }else{
//       next()
//     }
//     }else{
//       if(to.path != "/c-oj/login"){
//         next({path: "/c-oj/login"})
//       }else{
//         next()
//       }
//     }
//   }
// )
export default router
